package cn.sjxy.apiAdmin;

import cn.sjxy.baseLib.AjaxResult;
import cn.sjxy.baseLib.SjuTool;
import cn.sjxy.config.mvcExtend.NeedAdminLogin;
import cn.sjxy.mapper.Mp_OrderInfo;
import cn.sjxy.mapper.Mp_User;
import cn.sjxy.model.Md_OrderDetail;
import cn.sjxy.model.Md_OrderInfo;
import cn.sjxy.model.Md_User;
import cn.sjxy.service.Sv_OrderInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import java.util.Date;
import java.util.List;

/**
 * 管理员端 - 订单管理API
 */
@RestController
@Validated
@Api(tags = "管理员端-订单管理")
public class Ad_OrderInfo {

    @Autowired
    private Mp_OrderInfo mp_orderInfo;

    @Autowired
    private Mp_User mp_user;

    @Autowired
    private Sv_OrderInfo sv_orderInfo;

    /**
     * 获取订单列表（分页）
     *
     * @param orderCode      订单编号
     * @param orderDateStart 下单开始时间
     * @param orderDateEnd   下单结束时间
     * @param pageIndex      页码
     * @param pageSize       每页数量
     * @return
     */
    @GetMapping(value = "/admin/orderInfo/getTable")
    @ApiOperation("获取订单列表")
    @NeedAdminLogin
    public AjaxResult getTable(
            @RequestParam(required = false) String orderCode,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date orderDateStart,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date orderDateEnd,
            @RequestParam(defaultValue = "1") Integer pageIndex,
            @RequestParam(defaultValue = "10") Integer pageSize) {

        // 构建查询条件
        QueryWrapper<Md_OrderInfo> wrapper = new QueryWrapper<>();

        // 订单编号模糊查询
        if (SjuTool.isNotNull(orderCode) && !orderCode.trim().isEmpty()) {
            wrapper.like("orderCode", orderCode);
        }

        // 下单时间范围查询
        if (SjuTool.isNotNull(orderDateStart)) {
            wrapper.ge("orderDate", orderDateStart);
        }
        if (SjuTool.isNotNull(orderDateEnd)) {
            wrapper.le("orderDate", orderDateEnd);
        }

        // 按订单时间倒序
        wrapper.orderByDesc("orderDate");

        // 分页查询
        IPage<Md_OrderInfo> page = new Page<>(pageIndex, pageSize);
        page = mp_orderInfo.selectPage(page, wrapper);

        // 填充用户名称
        for (Md_OrderInfo orderInfo : page.getRecords()) {
            QueryWrapper<Md_User> userWrapper = new QueryWrapper<>();
            userWrapper.eq("userCode", orderInfo.getUserCode());
            Md_User user = mp_user.selectOne(userWrapper);
            if (user != null) {
                orderInfo.setUserName(user.getUserName());
            }
        }

        // 返回结果
        AjaxResult result = AjaxResult.success("查询成功");
        result.addTable(page.getRecords());
        result.addPage(page);
        return result;
    }

    /**
     * 获取订单详情
     *
     * @param orderCode
     * @return
     */
    @GetMapping(value = "/admin/orderInfo/getDetail")
    @ApiOperation("获取订单详情")
    @NeedAdminLogin
    public AjaxResult getDetail(@NotBlank(message = "订单编号不能为空") String orderCode) {
        Md_OrderInfo orderInfo = sv_orderInfo.getOrderDetail(orderCode);
        List<Md_OrderDetail> detailList = sv_orderInfo.getOrderDetailList(orderCode);

        AjaxResult result = AjaxResult.success("查询成功");
        result.addKey("orderInfo", orderInfo);
        result.addKey("detailList", detailList);
        return result;
    }

    /**
     * 订单发货
     *
     * @param orderCode
     * @param sendNo
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/admin/orderInfo/sendOrder")
    @ApiOperation("订单发货")
    @NeedAdminLogin
    public AjaxResult sendOrder(
            @NotBlank(message = "订单编号不能为空") String orderCode,
            @NotBlank(message = "物流单号不能为空") String sendNo) throws Exception {
        sv_orderInfo.sendOrder(orderCode, sendNo);
        return AjaxResult.success("发货成功");
    }
}

